﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ETALON
{
    public partial class listAny : Form
    {
        public  string sName, sCod, sText;
        public bool IsOk;

        ArrayList ar; int indexSelect;
        public listAny() { InitializeComponent(); IsOk = false; }
        public listAny(DataTable dt) : this()
        { ar = tableToArray(dt); sName = ""; sCod = ""; sText = ""; listBoxFill(ar);  }
        public listAny(DataView dv)
            : this()
        { ar = viewToArray(dv); sName = ""; sCod = ""; sText = ""; listBoxFill(ar); }
        public listAny(ArrayList al)
            : this()
        { ar = al; sName = ""; sCod = ""; sText = ""; listBoxFill(ar); }

        private ArrayList viewToArray(DataView dv)
        {
            ArrayList a = new ArrayList(); string s1, s2, s3;
            for (int i = 0; i < dv.Count; i++)
            {
                s1 = Convert.ToString(dv[i][1]); 
                s2 = s1.Replace("(", "/"); 
                s3 = s2.Replace(")", "/");

                a.Add(string.Concat(s3, " (", dv[i][0], ")"));
            }
            return a;
        }
        private ArrayList tableToArray(DataTable dt)
        {
            ArrayList a = new ArrayList(); string s1,s2,s3;
            foreach (DataRow r in dt.Rows) 
            {
                s1 = Convert.ToString(r[1]); s2 = s1.Replace("(", "/"); s3 = s2.Replace(")", "/");
                a.Add(string.Concat(s3, " (", r[0], ")")); 
            } 
            return a;
        }
        private void listBoxFill(ArrayList ar)
        {
            ar.Sort();  foreach(string ars in ar) listBox1.Items.Add(ars); 
        }
        private void RefreshListBoxWhithFilter(string skeyData)
        {
            listBox1.Items.Clear(); string sfilter = skeyData.ToLower(); //stext+skeyData
            ar.Sort();
            string sl = "";
          //  string s = "r4"; string ss = s.ToLower 
            #region old
            //for (int i = 0; i < ar.Count; i++)
            //{
            //    scurrent = ar[i].ToString();
            //    //System.Windows.Forms.MessageBox.Show(scurrent + "=scurrent" + scurrent.IndexOf(sfilter).ToString()+ "=scurrent.IndexOf");

            //    if (scurrent.IndexOf(sfilter) != -1)
            //    {
            //        listBox1.Items.Add(scurrent);
            //    }
            //}
            #endregion
            
            foreach (string scurrent in ar) 
            {
                sl = scurrent.ToLower();
                if (sl.IndexOf(sfilter) != -1) { listBox1.Items.Add(scurrent); } }
        }

        private void comboBox1_KeyUp(object sender, KeyEventArgs e)
        {
            sName = this.comboBox1.Text;    RefreshListBoxWhithFilter(sName); 
        }


        private void readSelect()
        {

            sText = listBox1.Text; sName = readName(sText); sCod = readCod(sText);
            //comboBox1.Text = this.sName;

            indexSelect = this.listBox1.SelectedIndex;  comboBox1.SelectAll();
            IsOk = true;
        }
        private string readName(string sText)
        {
            string s = sText, ss = "-"; int i; i = s.IndexOf("(") - 1;
            if (i > 0) { ss = s.Substring(0, i); } return ss;
        }
        private string readCod(string sText)
        {
            string s = sText, ss = "0"; int i, ii, il = s.Length; i = s.IndexOf("(") - 1; ii = il - (s.IndexOf("(") + 1) - 1;
            if (i > 0) { ss = s.Substring((s.IndexOf("(") + 1), ii); } return ss;
        }

        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            readSelect();  this.Close();
        }

    }
}
